﻿using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Collections.Generic;
using zdzh.zdmgr.COL;
using zdzh.zdmgr.SFL;

namespace zdzh.zdmgr.DAL.OleDb
{
    /// <summary>
    /// 对象名称：回迁户OleDb数据访问子类（数据访问层）
    /// 对象说明：提供“回迁户类（业务逻辑层）”针对OleDb的“增删改查”等各种数据访问方法，继承通用数据访问父类。
    /// 调用说明：通常不需要直接实例化本类，而使用“回迁户类（业务逻辑层）”中的DataAccess属性来调用本类所实现的方法。
    /// 作者姓名：飞翔熊
    /// 编写日期：2012-12-3 13:23:04
    /// </summary>
    public class HuiqianhuDAL:DAL.Common.HuiqianhuDAL
    {
        #region EasyCode所生成的默认代码
        //﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉
        //  此区域的代码为EasyCode所自动生成，实现了父类中定义的抽象方法。请不要直接修改该区域中的任何代码，   
        //  或在该区域中添加任何自定义代码，当该类发生变更时，您可以随时使用EasyCode重新生成覆盖其中的代码。  
        //﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍

        /// <summary>
        /// 将回迁户（Huiqianhu）数据，采用INSERT操作插入到数据库中，并返回受影响的行数。
        /// </summary>
        /// <param name="huiqianhu">回迁户（Huiqianhu）实例对象</param>
        public override int Insert(Huiqianhu huiqianhu)
        {
            if ( huiqianhu.IDNumber!=null && huiqianhu.IDNumber!="")
            {//已经存在不再次添加
                int? ret = GetDataByIDNumber(huiqianhu.IDNumber);
                if ( ret!=null)
                    return ret.Value;
            }
            int index = SFL.OleDbHelper.GetMaxIndex("huiqianhu")+1;
            string sqlText = "INSERT INTO [huiqianhu]"
                           + "([index],[xieyihao],[xingming],[idnumber],[beizhu])"
                           + "VALUES"
                           + "(@index,@xieyihao,@xingming,@idnumber,@beizhu)";
            OleDbParameter[] parameters = 
            {
                new OleDbParameter("@index" , OleDbType.Integer  , 4 ){ Value = index },
                new OleDbParameter("@xieyihao" , OleDbType.Integer  , 4 ){ Value = huiqianhu.Xieyihao },
                new OleDbParameter("@xingming" , OleDbType.VarWChar , 50){ Value = huiqianhu.Xingming },
                new OleDbParameter("@idnumber" , OleDbType.VarWChar , 50){ Value = huiqianhu.IDNumber },
                new OleDbParameter("@beizhu" , OleDbType.VarWChar  , 50 ){ Value = huiqianhu.Beizhu }
            };
            SFL.OleDbHelper.ExecuteNonQuery(sqlText, parameters);
            return index;
        }


        /// <summary>
        /// 将回迁户（Huiqianhu）数据，根据主键“协议号（Xieyihao）”采用UPDATE操作更新到数据库中，并返回受影响的行数。
        /// </summary>
        /// <param name="huiqianhu">回迁户（Huiqianhu）实例对象</param>
        public override int Update(Huiqianhu huiqianhu)
        {
            string sqlText = "UPDATE [huiqianhu] SET "
                           + "[xieyihao]=@xieyihao,[xingming]=@xingming,[idnumber]=@idnumber,[beizhu]=@beizhu "
                           + "WHERE [index]=@index";
            OleDbParameter[] parameters = 
            {
                new OleDbParameter("@xieyihao" , OleDbType.Integer  , 4 ){ Value = huiqianhu.Xieyihao },
                new OleDbParameter("@xingming" , OleDbType.VarWChar , 50){ Value = huiqianhu.Xingming },
                new OleDbParameter("@idnumber" , OleDbType.VarWChar , 50){ Value = huiqianhu.IDNumber },
                new OleDbParameter("@beizhu" , OleDbType.VarWChar , 50){ Value = huiqianhu.Beizhu },
                new OleDbParameter("@xingming" , OleDbType.Integer , 4){ Value = huiqianhu.Index }
            };
            return SFL.OleDbHelper.ExecuteNonQuery(sqlText, parameters);
        }


        /// <summary>
        /// 根据回迁户（Huiqianhu）的主键“协议号（Xieyihao）”采用DELETE操作从数据库中删除相关记录，并返回受影响的行数。
        /// </summary>
        /// <param name="xieyihao">回迁户（Huiqianhu）的主键“协议号（Xieyihao）”</param>
        public override int Delete(int xieyihao)
        {
            string sqlText = "DELETE FROM [huiqianhu] "
                           + "WHERE [xieyihao]=@xieyihao";
            OleDbParameter[] parameters = 
            {
                new OleDbParameter("@xieyihao" , OleDbType.Integer , 4){ Value = xieyihao }
            };
            return SFL.OleDbHelper.ExecuteNonQuery(sqlText, parameters);
        }

        public override int DeleteByIndex(int index)
        {
            string sqlText = "DELETE FROM [huiqianhu] "
                           + "WHERE [index]=@index";
            OleDbParameter[] parameters = 
            {
                new OleDbParameter("@index" , OleDbType.Integer , 4){ Value = index }
            };
            return SFL.OleDbHelper.ExecuteNonQuery(sqlText, parameters);
        }

        public override int? GetDataByIDNumber(string idnumber)
        {
            string sqlText = "SELECT * "
                           + "FROM [huiqianhu] "
                           + "WHERE [idnumber]=@idnumber";

            OleDbParameter[] parameters = 
            {
                new OleDbParameter("@idnumber" , OleDbType.VarChar , 50){ Value = idnumber }
            };

            int? ret = null;
            OleDbDataReader oleDbDataReader = SFL.OleDbHelper.ExecuteReader(sqlText, parameters);
            if (oleDbDataReader.Read())
                ret = Convert.ToInt32(oleDbDataReader["index"]);
            oleDbDataReader.Close();
            return ret;
        }


        /// <summary>
        /// 根据回迁户（Huiqianhu）的主键“协议号（Xieyihao）”从数据库中获取回迁户（Huiqianhu）的实例。
        /// 成功从数据库中取得记录返回新回迁户（Huiqianhu）的实例“，没有取到记录返回null值。
        /// </summary>
        /// <param name="xieyihao">回迁户（Huiqianhu）的主键“协议号（Xieyihao）”</param>

        public override List<Huiqianhu> GetDataByXieyihao(int xieyihao)
        {
            string sqlText = "SELECT * "
                           + "FROM [huiqianhu] "
                           + "WHERE [xieyihao]=@xieyihao";

            OleDbParameter[] parameters = 
            {
                new OleDbParameter("@xieyihao" , OleDbType.Integer , 4){ Value = xieyihao }
            };

            List<Huiqianhu> list = new List<Huiqianhu>();
            OleDbDataReader oleDbDataReader = SFL.OleDbHelper.ExecuteReader(sqlText, parameters);
            while (oleDbDataReader.Read())
            {
                Huiqianhu huiqianhu = new Huiqianhu();
                ReadHuiqianhuAllData(oleDbDataReader, huiqianhu);
                list.Add(huiqianhu);
            }
            oleDbDataReader.Close();
            return list;
        }

        public override Huiqianhu GetDataByIndex(int index)
        {
            Huiqianhu huiqianhu = null;
            string sqlText = "SELECT * "
                           + "FROM [huiqianhu] "
                           + "WHERE [index]=@index";
            OleDbParameter[] parameters = 
            {
                new OleDbParameter("@index" , OleDbType.Integer , 4){ Value = index }
            };

            OleDbDataReader oleDbDataReader = SFL.OleDbHelper.ExecuteReader(sqlText, parameters);
            if (oleDbDataReader.Read())
            {
                huiqianhu = new Huiqianhu();
                ReadHuiqianhuAllData(oleDbDataReader, huiqianhu);
            }
            oleDbDataReader.Close();
            return huiqianhu;
        }


        /// <summary>
        /// 从数据库中读取并返回所有回迁户（Huiqianhu）List列表。
        /// </summary>
        public override List<Huiqianhu> GetAllList()
        {
            string sqlText = "SELECT * "
                           + "FROM [huiqianhu]";
            List<Huiqianhu> list = new List<Huiqianhu>();
            OleDbDataReader oleDbDataReader = SFL.OleDbHelper.ExecuteReader(sqlText, null);
            while (oleDbDataReader.Read())
            {
                Huiqianhu huiqianhu = new Huiqianhu();
                ReadHuiqianhuAllData(oleDbDataReader, huiqianhu);
                list.Add(huiqianhu);
            }
            oleDbDataReader.Close();
            return list;
        }


        /// <summary>
        /// 根据每页记录数及所要获取的页数，从数据库中读取并返回经过分页后的回迁户（Huiqianhu）的列表及分页信息。
        /// 该方法所获取的回迁户（Huiqianhu）列表仅用于在数据控件中显示，该方法只为对象中需要显示的属性进行赋值。
        /// </summary>
        public override PageData GetPageList(int pageSize, int curPage)
        {
            string sqlText = "SELECT * "
                           + "FROM [huiqianhu]";
            List<Huiqianhu> list = new List<Huiqianhu>();
            OleDbDataReader oleDbDataReader = SFL.OleDbHelper.ExecuteReader(sqlText, null);

            PageData pageData = new PageData();
            pageData.PageSize = pageSize;
            pageData.CurPage = curPage;
            pageData.RecordCount = 0;
            pageData.PageCount = 1;

            int first = (curPage - 1) * pageSize + 1;
            int last = curPage * pageSize;

            while (oleDbDataReader.Read())
            {
                pageData.RecordCount++;
                if (pageData.RecordCount >= first && last >= pageData.RecordCount)
                {
                    Huiqianhu huiqianhu = new Huiqianhu();
                    ReadHuiqianhuPageData(oleDbDataReader, huiqianhu);
                    list.Add(huiqianhu);
                }
            }
            oleDbDataReader.Close();

            if(pageData.RecordCount>0)
                pageData.PageCount = Convert.ToInt32(Math.Ceiling((double)pageData.RecordCount / (double)pageSize));

            pageData.PageList = list;
            return pageData;
        }

        #endregion EasyCode所生成的默认代码

        //﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉
        //  说明：以下区域的代码为设计开发人员所编写，主要为扩展该数据访问类的功能，而定义的变量、属性及相关数据访问方法。  
        //  注意：为了保证该项目的多数据库支持与扩展性，请先在本类的父类中对相关抽象方法进行定义，再在本类中进行具体实现。
        //﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍


        public override bool IsIDNumberExist(string idnumber)
        {
            string sqlText = "SELECT * "
                           + "FROM [huiqianhu] "
                           + "WHERE [idnumber]=@idnumber";
            OleDbParameter[] parameters = 
            {
                new OleDbParameter("@idnumber" , OleDbType.VarChar , 50){ Value = idnumber }
            };

            OleDbDataReader oleDbDataReader = SFL.OleDbHelper.ExecuteReader(sqlText, parameters);
            return oleDbDataReader.Read();
        }

    }
}
